﻿@model SimpleTaskManager.Domain.Entities.Task
           
@using SimpleTaskManager.WebUI.HtmlHelpers;

@{
    ViewBag.Title = "Index";
}

@{ Html.RenderAction("Header", new { id = Model.TaskID, page = 0}); }
<fieldset>
    <legend>Sub Tasks</legend>
    
    <div id="SubtaskList"></div>

    @if(ViewBag.IsLeader){
        <div class="list_button">@Html.ActionLink("Add new task", "Create", new { parentId = Model.TaskID})</div>
    }
</fieldset>
@{ Html.RenderAction("Footer"); }

<div id="RequirementsDialog" title="" class="Hidden"></div>
<script type="text/javascript">
    $(function () {
        $("#RequirementsDialog").dialog({
            autoOpen: false, width: 400, height: 330, modal: true,
            buttons: {
                "Save":{
                    id: "save-button",
                    text: "Save",
                    click: function () {
                        //hide the save button and show the loader
                        $('#save-button').hide();
                        $('#ajaxBusy').show();

                        $.post("/Task/AddRequirement/" + $(this).data("id"),
                        $("#RequirementsForm").serialize(),
                        function () {
                            //show the save button and hide the loader
                            $('#save-button').show();
                            $('#ajaxBusy').hide();

                            $("#RequirementsDialog").dialog("close");
                            LoadList();
                        });
                    }
                },
                Cancel: function () { $(this).dialog("close"); }
            },
            create: function () {
                $('div.ui-dialog-buttonset').prepend('<div id="ajaxBusy" style="display:inline-block; margin: .5em 1.4em .5em 0; width:16px; height: 16px"><p><img src="../../Content/images/ajax-loader.gif"></p></div>');
                $('#ajaxBusy').hide();
            }
        });
        $(".AddReqLink").live("click", function () {
            var id = $(this).attr("taskid");
            $("#RequirementsDialog").html("")
                .dialog("option", "title", "Manage required tasks")
                .load("/Task/AddRequirement/" + id, function ()
                { $("#RequirementsDialog").data("id", id).dialog("open"); });
        });
        LoadList();
    });
    function LoadList() {
        $("#SubtaskList").load("/Task/SubtaskList/@(Model.TaskID)", function ()
        { $("#taskTable").dataTable({ bJQueryUI: true, sPaginationType: "full_numbers" });/*.tablesorter({ sortList: [[0, 0]] }); */ });
    }
</script>